<?php
/**
 * Created by allen.qiang cdd.
 * Date: 14-10-29
 * Time: 下午6:44
 * 发送邮件类
 * FUN-BK-0016
 */
header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require_once 'class.phpmailer.php';
class  sendMail{


    /*
     * 发送邮件
     * @package  module
     * @subpackage  base
     * @author   allen.qiang <allen.qiang@fistforward.com>
     *$arg_array['subject']  邮件标题
	 *$arg_array['message']  邮件正文内容
	 *$arg_array['from']  发件人例：
	 *$arg_array['fromName']  发件人名
	 *$arg_array['to']  收件人，数组
	 *$arg_array['cc']  抄送，数组
	 *$arg_array['reply']  回复 数组
     */
    public function send(&$arg_array) {
        try{
            $mail = new PHPMailer(true);
            $mail->IsSMTP();
            $mail->CharSet='UTF-8'; //设置邮件的字符编码，这很重要，不然中文乱码
            $mail->SMTPAuth = true; //开启认证
            $mail->Port = G_MAIL_PORT;
            $mail->Host = G_MAIL_HOST;
            $mail->Username = G_MAIL_ADDRESS;
            $mail->Password = G_MAIL_PASSWORD;
            //$mail->IsSendmail(); //如果没有sendmail组件就注释掉，否则出现“Could not execute: /var/qmail/bin/sendmail ”的错误提示
            //$mail->AddReplyTo($arg_array['reply'],'测试');//回复地址
            $mail->From = G_MAIL_ADDRESS;       //发件人
            $mail->FromName =$arg_array['fromName'];//发件人名
            foreach($arg_array['to'] as $to){
                $array=explode(',',$to->itemName);
                $mail->AddAddress($array[0],$array[1]); //收件人
            }
            if(!empty($arg_array['cc'])){
                foreach($arg_array['cc'] as $to_cc){
                    $mail->AddBCC($to_cc->itemName,$to_cc->templateName);//抄送人 加 抄送标题
                }
            }

            $mail->Subject = $arg_array['subject'];//邮件标题
            $mail->Body =$arg_array['message'];     //邮件正文内容
            $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示，可以省略
            $mail->WordWrap = 80; // 设置每行字符串的长度
            //$mail->AddAttachment(); //添加附件
            $mail->IsHTML(true);

            $isSuc=@$mail->Send();
            if($isSuc==false){
                return '发送失败';
            }else{
                return '邮件已发送';
            }
        } catch (phpmailerException $e) {
            echo "邮件发送失败：".$e->errorMessage();
        }

    }









}